package com.hk.web.context;

import com.hk.commons.domain.service.ValidateService;
import com.hk.commons.enums.FeatureToggleType;
import com.hk.commons.enums.Whether;
import com.hk.commons.exceptions.SpecificException;
import com.hk.web.domain.entity.Merchant;
import com.hk.web.domain.entity.MerchantFeatureToggle;
import com.hk.web.domain.entity.MerchantUserAccount;
import com.hk.web.domain.entity.User;
import com.hk.web.domain.lifeCycle.factory.MerchantFactory;
import com.hk.web.domain.lifeCycle.repository.MerchantFeatureToggleRepository;
import com.hk.web.domain.lifeCycle.repository.MerchantRepository;
import com.hk.web.domain.lifeCycle.repository.MerchantUserAccountRepository;
import com.hk.web.dto.merchant.QueryAccountDTO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestBody;

import java.util.List;


/**
 * @author Antinomy
 * @date 17/11/2
 */
@Service
public class MerchantContext {

    @Autowired
    private MerchantRepository merchantRepo;

    @Autowired
    private MerchantUserAccountRepository accountRepo;

    @Autowired
    private MerchantFeatureToggleRepository featureToggleRepo;


    public Merchant getMerchantByKey(String merchantKey) {
        Merchant merchant = merchantRepo.findByMerchantKeyAndEnable(merchantKey, Whether.YES);

        if (merchant == null) {
            throw new SpecificException("merchantKey: " + merchantKey + " 不存在或未启用");
        }

        return merchant;
    }


    public Merchant getMerchantByName(String merchantName) {
        Merchant merchant = merchantRepo.findByNameAndEnable(merchantName, Whether.YES);

        if (merchant == null) {
            throw new SpecificException("merchantName: " + merchantName + " 不存在或未启用");
        }
        return merchant;
    }

    public void validateCommonParams(@RequestBody QueryAccountDTO dto, ValidateService validate) {
        validate.checkAndPrepare("merchantKey", dto.getMerchantKey());
        validate.checkAndPrepare("httpMethod", dto.getHttpMethod());
        validate.checkAndPrepare("randomCode", dto.getRandomCode());
        validate.checkAndPrepare("signVersion", dto.getSignVersion());
        validate.checkAndPrepare("ts", dto.getTs());
        validate.checkAndPrepare("sign", dto.getSign());
    }


    public MerchantUserAccount getOrCreateUserAccount(Merchant merchant, User user) {

        MerchantUserAccount merchantUserAccount = accountRepo.findByMerchantKeyAndMobile(merchant.getMerchantKey(), user.getName());

        if (merchantUserAccount == null) {
            merchantUserAccount = MerchantFactory.createMerchantUserAccount(merchant, user);
            merchantUserAccount = accountRepo.saveAndFlush(merchantUserAccount);
        }

        return merchantUserAccount;
    }


    public MerchantUserAccount findUserByAccountId(Merchant merchant, String accountId) {

        MerchantUserAccount merchantUserAccount = accountRepo.findByMerchantKeyAndMerchantAccountIdAndEnable(
                merchant.getMerchantKey(), accountId, Whether.YES);

        if (merchantUserAccount == null) {
            throw new SpecificException("accountId: " + accountId + " 不存在或未启用");
        }

        return merchantUserAccount;
    }

    public List<MerchantFeatureToggle> findAllFeatureToogle(){
        return featureToggleRepo.findAll();
    }

    public MerchantFeatureToggle findMerchantFeature(FeatureToggleType type) {
        MerchantFeatureToggle featureToggle = featureToggleRepo.findByFeature(type);
        return featureToggle;
    }
}
